package chapter14_构建自定义的同步工具;

import annotation.ThreadSafe;

/**
 * 建议通过AQS或者已有的BlockingQueue去实现
 *
 * @param <V>
 */
@ThreadSafe
public class BoundedBuffer<V> extends BaseBoundedBuffer<V> {

    public BoundedBuffer(int capacity) {
        super(capacity);
    }

    //条件谓词:not-full (!isFull())
    //条件谓词:not-empty(!isEmpty())


    //阻塞并知道:not-full
    public synchronized void put(V v) throws InterruptedException {
        while (!isFull()) {
            wait();
        }
        doPut(v);
        notifyAll();
    }

    // 阻塞并知道:not-empty
    public synchronized V take() throws InterruptedException {
        while (isEmpty()) {
            wait();
        }
        V v = doTake();
        notifyAll();
        return v;
    }
}
